<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工" prop="partyId">
        <el-select v-model="queryParams.partyId" placeholder="请选择员工" clearable filterable>
          <el-option v-for="dict in partyIdOptions" :key="dict.partyId" :label="dict.firstName" :value="dict.partyId" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同类型" prop="contractTypeId">
        <el-select v-model="queryParams.contractTypeId" placeholder="请选择合同类型" clearable>
          <el-option v-for="dict in contractTypeIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同状态" prop="cotractStatusId">
        <el-select v-model="queryParams.cotractStatusId" placeholder="请选择合同状态" clearable>
          <el-option v-for="dict in cotractStatusIdOptions" :key="dict.enumCode" :label="dict.description"
            :value="dict.enumCode" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini">
          <router-link :to="'/workflow/approve/employ_contract/apply'" class="link-type">
            <span>员工合同签订申请</span>
          </router-link>
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:employ_contract:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employ_contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contractId" />
      <el-table-column label="员工" align="center" prop="partyId" width="100" :formatter="partyIdFormat" />
      <el-table-column label="合同类型" align="center" prop="contractTypeId" width="120" :formatter="contractTypeIdFormat" />
      <el-table-column label="签订日期" align="center" prop="signContractDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signContractDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="contractStartDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="contractEndDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="cotractStatusId" width="100"
        :formatter="cotractStatusIdFormat" />
      <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:employ_contract:detail']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改员工合同对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工" prop="partyId">
              <span>{{ personFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同类型" prop="contractTypeId">
              <span>{{ contractTypeIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签订日期" prop="signContractDate">
              <span>{{ form.signContractDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="contractStartDate">
              <span>{{ form.contractStartDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="contractEndDate">
              <span>{{ form.contractEndDate }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="备注" prop="comments">
              <span>{{ form.comments }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同附件" prop="affixFilePath">
              <file-upload v-model="form.affixFilePath" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同状态" prop="cotractStatusId">
              <span>{{ cotractStatusIdFormat(form) }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmploy_contract, getEmploy_contract, delEmploy_contract, addEmploy_contract, updateEmploy_contract } from "@/api/ehr/employ_contract";
// person enum api
import { getPersonEnum } from "@/api/ehr/person";
export default {
  name: "Employ_contract",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工合同表格数据
      employ_contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 人员字典
      personOptions: [],
      // 员工编号业务字典
      partyIdOptions: [],
      // 合同类型业务字典
      contractTypeIdOptions: [],
      // 合同状态业务字典
      cotractStatusIdOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        partyId: null,
        contractTypeId: null,
        cotractStatusId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        partyId: [
          { required: true, message: "员工不能为空", trigger: "change" }
        ],
        contractTypeId: [
          { required: true, message: "合同类型不能为空", trigger: "change" }
        ],
        signContractDate: [
          { required: true, message: "签订日期不能为空", trigger: "blur" }
        ],
        contractStartDate: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ],
        contractEndDate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // 人员字典
    getPersonEnum().then((response) => {
      this.personOptions = response.data;
    });
    // 员工编号业务字典
    this.getEntityDicts("person:partyId:firstName").then((response) => {
      this.partyIdOptions = response.data;
    });
    // 合同类型业务字典
    this.getEnumDicts("EMPL_CONTRACT_TYPE").then((response) => {
      this.contractTypeIdOptions = response.data;
    });
    // 合同状态业务字典
    this.getEnumDicts("ACTIVE_STOP").then((response) => {
      this.cotractStatusIdOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 人员字典转换
    personFormat(row, column) {
      let val = null;
      this.personOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.name;
        }
      });
      return val;
    },
    // 审批人字典转换
    approverFormat(row, column) {
      let val = null;
      this.personOptions.forEach((item, index) => {
        if (row.approver === item.userId + "") {
          val = item.firstName;
        }
      });
      return val;
    },
    partyIdFormat(row, column) {
      let val = null;
      this.partyIdOptions.forEach((item, index) => {
        if (row.partyId === item.partyId) {
          val = item.firstName;
        }
      });
      return val;
    },
    contractTypeIdFormat(row, column) {
      let val = null;
      this.contractTypeIdOptions.forEach((item, index) => {
        if (row.contractTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    cotractStatusIdFormat(row, column) {
      let val = null;
      this.cotractStatusIdOptions.forEach((item, index) => {
        if (row.cotractStatusId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询员工合同列表 */
    getList() {
      this.loading = true;
      listEmploy_contract(this.queryParams).then(response => {
        this.employ_contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        partyId: null,
        contractName: null,
        contractTypeId: null,
        signContractDate: null,
        contractStartDate: null,
        contractEndDate: null,
        comments: null,
        affixFilePath: null,
        cotractStatusId: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null,
        deptId: null,
        orgId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加员工合同";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const contractId = row.contractId || this.ids
      getEmploy_contract(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "员工合同详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const contractId = row.contractId || this.ids
      getEmploy_contract(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null && this.updateFlag) {
            updateEmploy_contract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmploy_contract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除员工合同编号为"' + contractIds + '"的数据项？').then(function () {
        return delEmploy_contract(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/employ_contract/export', {
        ...this.queryParams
      }, `employ_contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
